/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GradientPanel.java
 *
 * Created on Mar 26, 2010, 3:27:09 PM
 */
package buseylab.fiteyemodel.gui;

/**
 *
 * @author ruj
 */
public class GradientPanel extends javax.swing.JPanel {

    private void handleBoxSizeChange() {
        if (this.listener != null) {
            this.listener.gradientBoxSizeChange(this.heightSlider.getValue(), this.widthSlider.getValue());
        }
    }

    private void handleDarkestCornerChange() {
        if (this.listener != null) {
            this.listener.darkestCornerChange(getDarkestCorner());
        }
    }

    public enum Corner {

        TOPLEFT, TOPRIGHT, BOTTOMLEFT, BOTTOMRIGHT
    }

    public interface GradientPanelListener {

        public void enableGradientCorrection(boolean enable);

        public void gradientBoxSizeChange(int height, int width);

        public void darkestCornerChange(Corner corner);

        public void brightnessIncreaseChange(int brightnessIncrease);

        public void onlyShowGradient(boolean enable);

        public void backgroundLevelIncreaseChange(int value);
    }
    
    GradientPanelListener listener = null;

    public void setListener(GradientPanelListener listener) {
        this.listener = listener;
    }

    public GradientPanelListener getListener() {
        return listener;
    }

    /** Creates new form GradientPanel */
    public GradientPanel() {
        initComponents();
    }

    public void enableGradientCorrection(boolean enable) {
        this.enableCheckBox.setSelected(enable);
    }

    public boolean isGradientCorrectionEnable() {
        return this.enableCheckBox.isSelected();
    }

    public void setGradientBoxSize(int height, int width) {
        this.heightSlider.setValue(height);
        this.widthSlider.setValue(width);
    }

    public int getGradientBoxHeight() {
        return this.heightSlider.getValue();
    }

    public int getGradientBoxWidth() {
        return this.widthSlider.getValue();
    }

    public void setDarkestCorner(Corner corner) {
        switch (corner) {
            case BOTTOMLEFT:
                this.bottomLeftRadioButton.setSelected(true);
                break;
            case BOTTOMRIGHT:
                this.bottomRightRadioButton.setSelected(true);
                break;
            case TOPRIGHT:
                this.topRightRadioButton.setSelected(true);
                break;
            default:
                this.topLeftRadioButton.setSelected(true);
        }
    }

    public Corner getDarkestCorner() {
        Corner corner = Corner.TOPLEFT;
        if (this.topRightRadioButton.isSelected()) {
            corner = Corner.TOPRIGHT;
        } else if (this.bottomLeftRadioButton.isSelected()) {
            corner = Corner.BOTTOMLEFT;
        } else if (this.bottomRightRadioButton.isSelected()) {
            corner = Corner.BOTTOMRIGHT;
        }
        return corner;
    }

    public void setBrightnessIncrease(int v) {
        this.brightnessIncreaseSlider.setValue(v);
    }

    public int getBrightnessIncrease() {
        return this.brightnessIncreaseSlider.getValue();
    }

    public void setBackGroundLevel(int v) {
        this.backgroundLevelSlider.setValue(v);
    }

    public int getBackGroundLevel() {
        return this.backgroundLevelSlider.getValue();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cornerButtonGroup = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        enableCheckBox = new javax.swing.JCheckBox();
        jPanel4 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        widthLabel = new javax.swing.JLabel();
        heightLabel = new javax.swing.JLabel();
        heightSlider = new javax.swing.JSlider();
        widthSlider = new javax.swing.JSlider();
        showGradientCheckBox = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        bottomRightRadioButton = new javax.swing.JRadioButton();
        topLeftRadioButton = new javax.swing.JRadioButton();
        bottomLeftRadioButton = new javax.swing.JRadioButton();
        topRightRadioButton = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        brightnessIncreaseSlider = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        backgroundLevelSlider = new javax.swing.JSlider();

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );

        setBackground(new java.awt.Color(255, 255, 255));
        setOpaque(false);

        enableCheckBox.setText("Enable Gradient Correction");
        enableCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                enableCheckBoxStateChanged(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel4.setEnabled(false);
        jPanel4.setOpaque(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setOpaque(false);

        widthLabel.setText("Width"); // NOI18N

        heightLabel.setText("Height"); // NOI18N

        heightSlider.setMaximum(512);
        heightSlider.setValue(100);
        heightSlider.setEnabled(false);
        heightSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                heightSliderStateChanged(evt);
            }
        });

        widthSlider.setMaximum(512);
        widthSlider.setValue(100);
        widthSlider.setEnabled(false);
        widthSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                widthSliderStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, heightLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, widthLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(heightSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                    .add(widthSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(widthSlider, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(widthLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(heightLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(heightSlider, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        showGradientCheckBox.setText("Show gradient only");
        showGradientCheckBox.setEnabled(false);
        showGradientCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                showGradientCheckBoxStateChanged(evt);
            }
        });

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Darkest Corner:"));
        jPanel3.setEnabled(false);
        jPanel3.setOpaque(false);

        cornerButtonGroup.add(bottomRightRadioButton);
        bottomRightRadioButton.setText("Bottom Right");
        bottomRightRadioButton.setEnabled(false);
        bottomRightRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bottomRightRadioButtonActionPerformed(evt);
            }
        });

        cornerButtonGroup.add(topLeftRadioButton);
        topLeftRadioButton.setSelected(true);
        topLeftRadioButton.setText("Top Left");
        topLeftRadioButton.setEnabled(false);
        topLeftRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                topLeftRadioButtonActionPerformed(evt);
            }
        });

        cornerButtonGroup.add(bottomLeftRadioButton);
        bottomLeftRadioButton.setText("Bottom Left");
        bottomLeftRadioButton.setEnabled(false);
        bottomLeftRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bottomLeftRadioButtonActionPerformed(evt);
            }
        });

        cornerButtonGroup.add(topRightRadioButton);
        topRightRadioButton.setText("Top Right");
        topRightRadioButton.setEnabled(false);
        topRightRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                topRightRadioButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, topLeftRadioButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, bottomLeftRadioButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(bottomRightRadioButton)
                    .add(topRightRadioButton)))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(topLeftRadioButton)
                    .add(topRightRadioButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(bottomLeftRadioButton)
                    .add(bottomRightRadioButton)))
        );

        jLabel1.setText("Brightness Increase");

        brightnessIncreaseSlider.setMaximum(255);
        brightnessIncreaseSlider.setValue(0);
        brightnessIncreaseSlider.setEnabled(false);
        brightnessIncreaseSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                brightnessIncreaseSliderStateChanged(evt);
            }
        });

        jLabel2.setText("Background Level");

        backgroundLevelSlider.setMaximum(255);
        backgroundLevelSlider.setValue(0);
        backgroundLevelSlider.setEnabled(false);
        backgroundLevelSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                backgroundLevelSliderStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(showGradientCheckBox)
                        .addContainerGap())
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(brightnessIncreaseSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE))
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 121, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(backgroundLevelSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel4Layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(30, 30, 30)
                        .add(showGradientCheckBox)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(brightnessIncreaseSlider, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(backgroundLevelSlider, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(9, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(enableCheckBox)
                    .add(layout.createSequentialGroup()
                        .add(8, 8, 8)
                        .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(enableCheckBox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 258, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void widthSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_widthSliderStateChanged
        handleBoxSizeChange();
}//GEN-LAST:event_widthSliderStateChanged

    private void heightSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_heightSliderStateChanged
        handleBoxSizeChange();
}//GEN-LAST:event_heightSliderStateChanged

    private void topLeftRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topLeftRadioButtonActionPerformed
        handleDarkestCornerChange();
    }//GEN-LAST:event_topLeftRadioButtonActionPerformed

    private void topRightRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topRightRadioButtonActionPerformed
        handleDarkestCornerChange();
    }//GEN-LAST:event_topRightRadioButtonActionPerformed

    private void bottomLeftRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bottomLeftRadioButtonActionPerformed
        handleDarkestCornerChange();
    }//GEN-LAST:event_bottomLeftRadioButtonActionPerformed

    private void bottomRightRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bottomRightRadioButtonActionPerformed
        handleDarkestCornerChange();
    }//GEN-LAST:event_bottomRightRadioButtonActionPerformed

    private void brightnessIncreaseSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_brightnessIncreaseSliderStateChanged
        if (this.listener != null) {
            this.listener.brightnessIncreaseChange(this.brightnessIncreaseSlider.getValue());
        }
    }//GEN-LAST:event_brightnessIncreaseSliderStateChanged

    private void showGradientCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_showGradientCheckBoxStateChanged
        if(this.listener != null){
            this.listener.onlyShowGradient(this.showGradientCheckBox.isSelected());
        }
    }//GEN-LAST:event_showGradientCheckBoxStateChanged

    private void enableCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_enableCheckBoxStateChanged
        boolean enable = this.enableCheckBox.isSelected();

        if (this.listener != null) {
            this.listener.enableGradientCorrection(enable);
        }

        // Enable components according to the state of the chack box
        this.bottomLeftRadioButton.setEnabled(enable);
        this.bottomRightRadioButton.setEnabled(enable);
        this.topLeftRadioButton.setEnabled(enable);
        this.topRightRadioButton.setEnabled(enable);
        this.showGradientCheckBox.setEnabled(enable);
        this.brightnessIncreaseSlider.setEnabled(enable);
        this.backgroundLevelSlider.setEnabled(enable);
        this.widthSlider.setEnabled(enable);
        this.heightSlider.setEnabled(enable);
    }//GEN-LAST:event_enableCheckBoxStateChanged

    private void backgroundLevelSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_backgroundLevelSliderStateChanged
         if (this.listener != null) {
            this.listener.backgroundLevelIncreaseChange(this.backgroundLevelSlider.getValue());
        }
    }//GEN-LAST:event_backgroundLevelSliderStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSlider backgroundLevelSlider;
    private javax.swing.JRadioButton bottomLeftRadioButton;
    private javax.swing.JRadioButton bottomRightRadioButton;
    private javax.swing.JSlider brightnessIncreaseSlider;
    private javax.swing.ButtonGroup cornerButtonGroup;
    private javax.swing.JCheckBox enableCheckBox;
    private javax.swing.JLabel heightLabel;
    private javax.swing.JSlider heightSlider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JCheckBox showGradientCheckBox;
    private javax.swing.JRadioButton topLeftRadioButton;
    private javax.swing.JRadioButton topRightRadioButton;
    private javax.swing.JLabel widthLabel;
    private javax.swing.JSlider widthSlider;
    // End of variables declaration//GEN-END:variables
}
